package com.itheima.Demo;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.AsynchronousServerSocketChannel;
import java.nio.channels.AsynchronousSocketChannel;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

public class DemoAIO同步连接001 {
    public static void main(String[] args) throws IOException, ExecutionException, InterruptedException {
        //创建对象
        AsynchronousServerSocketChannel assc = AsynchronousServerSocketChannel.open();
        //绑定端口
        assc.bind(new InetSocketAddress(9999));
        //连接客户端(同步连接)
        Future<AsynchronousSocketChannel> accept = assc.accept();
        //Future就像是一个盒子,真正的结果在盒子里面
        AsynchronousSocketChannel asc = accept.get();
        //接受数据
        ByteBuffer buffer = ByteBuffer.allocate(1024);
        //读取数据
        Future<Integer> read = asc.read(buffer);
        //真正读取到的字节个数在future盒子里面放着
        Integer len = read.get();
        //打印
        System.out.println(new String(buffer.array(),0,len));

    }
}
